package br.com.infocruz.seam.crm.util;

public class Pagination {
	private static final int DEFAULT_PAGE_SIZE = 25;	
	
    private int pageSize;
	private int page = 1;
    private int itemsCount;

    public Pagination() {
    	pageSize = DEFAULT_PAGE_SIZE;
	}
    
    public Pagination(int pageSize) {
    	this.pageSize = pageSize;
    }

	public int getPageFirstItem() {
        return (page-1)*pageSize;
    }

	public int getPageLastItem() {
        int i = getPageFirstItem() + pageSize -1;
        int count = getItemsCount() - 1;
        if (i > count) {
        }
        i = count;
        if (i < 0) {
            i = 0;
        }
        return i;
    }
	
    public void firstPage() {
    	page = 1;
    }

    public boolean isHasNextPage() {
        return (page)*pageSize+1 <= getItemsCount();
    }

    public void nextPage() {
        if (isHasNextPage()) {
            page++;
        }
    }

    public boolean isHasPreviousPage() {
        return page > 1;
    }

    public void previousPage() {
        if (isHasPreviousPage()) {
            page--;
        }
    }
    
    public void lastPage() {
    	page = getItemsCount() / pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }
    
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
    public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}	
	
    public int getItemsCount() {
		return itemsCount;
	}

	public void setItemsCount(int itemsCount) {
		this.itemsCount = itemsCount;
	}
	
	public int getPreviousFirstResult() {
		return getPageFirstItem() - pageSize;
	}

	public int getPreviousPage() {
		return page - 1;
	}
	
	public int getNextFirstResult() {
		return getPageFirstItem() + getPageSize();
	}
	
	public int getNextPage() {
		return page + 1;
	}	
	
	public static void main(String[] args) {
		Pagination pagination = new Pagination(25);
		pagination.setItemsCount(125);
		System.out.println(pagination.getPage());
		System.out.println(pagination.getPageFirstItem());
		System.out.println(pagination.getPageLastItem());
		System.out.println(pagination.isHasPreviousPage());
		
		pagination.nextPage();
		System.out.println(pagination.getPage());
		System.out.println(pagination.getPageFirstItem());
		System.out.println(pagination.getPageLastItem());
		System.out.println(pagination.isHasPreviousPage());
		
		pagination.firstPage();
		System.out.println(pagination.getPage());
		System.out.println(pagination.getPageFirstItem());
		System.out.println(pagination.getPageLastItem());		

		pagination.lastPage();
		System.out.println(pagination.getPage());
		System.out.println(pagination.getPageFirstItem());
		System.out.println(pagination.getPageLastItem());		

		pagination.previousPage();
		System.out.println(pagination.getPage());
		System.out.println(pagination.getPageFirstItem());
		System.out.println(pagination.getPageLastItem());		
		
		System.out.println(pagination.isHasNextPage());
		
		pagination.lastPage();
		
		System.out.println(pagination.isHasNextPage());
		
	}
}